package spring.utils;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

@Slf4j
public class CronUtils {
    private static final SimpleDateFormat sdfCron = new SimpleDateFormat("0 mm HH * * ?");
    private static final SimpleDateFormat sdfString = new SimpleDateFormat("HH:mm");

    /**
     *  功能描述：日期转换cron表达式
     * @param date
     * @return
     */
    public static String formatDateByPattern(Date date) {
        String formatTimeStr = null;
        if (Objects.nonNull(date)) {
            formatTimeStr = sdfCron.format(date);
        }
        return formatTimeStr;
    }

    /**
     * @param date  : 字符串
     * @return
     */
    public static String getCron(String date) {
        try {
            Date parse = sdfString.parse(date);
            return formatDateByPattern(parse);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws ParseException {
        System.out.println(getCron("8:00"));
    }
}
